home *** CD-ROM | disk | FTP | other *** search
/ bioinformatics.org / bioinformatics.org_software.tar / www.bioinformatics.org / download / ecell2 / ecell220setup.exe / {app} / standard / SRCJ / Substance.java < prev    next >
Text File  |  2002-05-13  |  15KB  |  525 lines

  1.  
  2. /**
  3.  * title:     Substance class (Substance.java)<p>
  4.  * description: Substance window class<p>
  5.  * Copyright (C) 1996-2001 Keio University <p>
  6.  * Copyright (C) 1998-2001 Japan Science and Technology Corporation (JST)<p>
  7.  *               GNU General Public Licence <p>
  8.  * Division:     Mitui Knowledge Industry Co. Ltd. Bioscience division <p>
  9.  * Version :     $Id: Substance.java,v 1.7 2002/05/13 00:23:04 ota Exp $ <p>
  10. */
  11.  
  12.  
  13. package ecell;
  14.  
  15. import java.awt.*;
  16. import java.awt.event.*;
  17. import javax.swing.border.*;
  18. import javax.swing.*;
  19. import javax.swing.plaf.basic.*;
  20. import java.io.*;
  21. import java.util.Vector;
  22.  
  23. /**
  24.  * Substance window class
  25.  * @author      XD.Zheng
  26.  * @version     1.0
  27.  * @since       JDK1.2.2
  28.  */
  29.  
  30. /**
  31. public class Substance extends JFrame
  32.     implements ActionListener, ItemListener, FocusListener, KeyListener, AccessListener{
  33. **/
  34.  
  35. public class Substance extends ECell2Interface implements ItemListener, FocusListener
  36. {
  37.  
  38.     JPanel jp1 = new JPanel();
  39.     JPanel jp11 = new JPanel();
  40.     JPanel jp12 = new JPanel();
  41.     JPanel jp2 = new JPanel();
  42.     JPanel jp31 = new JPanel();
  43.     JPanel jp32 = new JPanel();
  44.     JPanel jp311 = new JPanel();
  45.     JPanel jp321 = new JPanel();
  46.     JPanel jp3 = new JPanel();
  47.  
  48.     JLabel label1 = new JLabel();
  49.     JLabel label2 = new JLabel();
  50.     JLabel label3 = new JLabel("Quantity[molecules]");
  51.  
  52.     JTextField txtQuantity= new JTextField(30);
  53.     JLabel label5 = new JLabel("Concentration[M]");
  54.     JTextField txtConcent = new JTextField(30);
  55.     JButton buttonP = new JButton("+");
  56.     JButton buttonM = new JButton("-");
  57.     JCheckBox checkFix = new JCheckBox("FIX");
  58.     JButton buttonClose= new JButton("Close");
  59.     Border border1;
  60.     Border border2;
  61.     Border border3;
  62.     GridLayout gridLayout1 = new GridLayout();
  63.  
  64.     //boolean focusFlag1 =false;
  65.     //boolean focusFlag2 =false;
  66.  
  67.     // 2002.1.22 akira shiozawa
  68.     // last value for quantity/concentration
  69.     String value_backup = "";
  70.  
  71.  
  72.     /**
  73.      * Instance of MainWindow
  74.      */
  75.     //MainWindow window;
  76.  
  77.     /**
  78.     * Timer to determine updating data
  79.     */
  80.     //Timer timer = new Timer(100,this);
  81.  
  82.     /**
  83.      * SubstanceWindows number
  84.      */
  85.     int subCode =-1;
  86.  
  87.     /**
  88.      *EntryName;
  89.      */
  90.  
  91.     String entryName ="";
  92.     /**
  93.      *Name;
  94.      */
  95.     String name = "";
  96.  
  97.  
  98.  
  99.     /**
  100.      *Constructor
  101.      */
  102.     public Substance( int code,String entryName, String name,MainWindow window) {
  103.     
  104.         super( window );
  105.     
  106.         enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  107.         this.subCode =code;
  108.         this.entryName= entryName;
  109.         this.name = name;
  110.         //this.window = window;
  111.         
  112.         // 2002.2.21 comment out by akira shiozawa
  113.         //int length = entryName.length();
  114.         //String str = entryName.substring(length-2,length);
  115.  
  116.         jbInit();
  117.             //timer.start();
  118.         window.addAccessListener( this );
  119.     }
  120.  
  121.  
  122.     /**
  123.      * Initialization
  124.      * @return void
  125.      */
  126.     public void jbInit(){
  127.         this.setResizable(true);
  128.         border1 = BorderFactory.createCompoundBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.white,new Color(142, 142, 142),new Color(99, 99, 99)),BorderFactory.createEmptyBorder(0,3,0,3));
  129.         border2 = BorderFactory.createCompoundBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.white,new Color(99, 99, 99),new Color(142, 142, 142)),BorderFactory.createEmptyBorder(0,3,0,3));
  130.         border3 = BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.white,new Color(142, 142, 142),new Color(99, 99, 99));
  131.         //this.setSize(new Dimension(350, 200));
  132.         this.setSizeAndLocation(new Dimension(350, 200), new Point(150, 100) );
  133.         this.setTitle("Substance: " + entryName);
  134.  
  135.         buttonClose.addActionListener(this);
  136.         buttonP.addActionListener(this);
  137.         buttonM.addActionListener(this);
  138.  
  139.         jp11.setLayout(new FlowLayout(0,5,2));
  140.         buttonClose.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.white,new Color(142, 142, 142),new Color(99, 99, 99)),BorderFactory.createEmptyBorder(2,5,2,5)));
  141.         label1.setForeground(Color.black);
  142.         label2.setForeground(Color.black);
  143.  
  144.         buttonP.setFont(new java.awt.Font("DialogInput", 1, 12));
  145.         buttonP.setForeground(SystemColor.activeCaption);
  146.         buttonP.setBorder(border2);
  147.         buttonM.setFont(new java.awt.Font("DialogInput", 1, 12));
  148.         buttonM.setForeground(SystemColor.activeCaption);
  149.         buttonM.setBorder(border1);
  150.         jp2.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.white,new Color(142, 142, 142),new Color(99, 99, 99)));
  151.         jp31.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.white,new Color(142, 142, 142),new Color(99, 99, 99)));
  152.         jp32.setBorder(border3);
  153.         txtQuantity.addFocusListener(this);
  154.         // 2002.1.23 akira shiozawa
  155.         txtQuantity.addKeyListener( myKeyAdapter );
  156.         //txtQuantity.setForeground(SystemColor.desktop);
  157.         //txtQuantity.setForeground(Color.lightGray);
  158.         txtQuantity.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.white,Color.white,new Color(142, 142, 142),new Color(99, 99, 99)),BorderFactory.createEmptyBorder(0,10,0,0)));
  159.         txtQuantity.setHorizontalAlignment(SwingConstants.LEFT);
  160.         txtConcent.addFocusListener(this);
  161.         // 2002.1.23 akira shiozawa
  162.         txtConcent.addKeyListener( myKeyAdapter );
  163.         //txtConcent.setForeground(SystemColor.desktop);
  164.         //txtConcent.setForeground(Color.lightGray);
  165.         txtConcent.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.white,Color.white,new Color(142, 142, 142),new Color(99, 99, 99)),BorderFactory.createEmptyBorder(0,10,0,0)));
  166.         label3.setFont(new java.awt.Font("Dialog", 3, 12));
  167.         label3.setForeground(Color.black);
  168.         label3.setBorder(BorderFactory.createEmptyBorder(0,10,0,0));
  169.         label5.setFont(new java.awt.Font("Dialog", 3, 12));
  170.         label5.setForeground(Color.black);
  171.         label5.setBorder(BorderFactory.createEmptyBorder(0,10,0,0));
  172.         jp11.add(checkFix);
  173.         checkFix.addActionListener(this);
  174.         jp12.setLayout(new FlowLayout(2,15,2));
  175.         jp12.add(buttonClose);
  176.         jp1.setLayout(new BorderLayout());
  177.         jp1.add(jp11,"West");
  178.         jp1.add(jp12,"East");
  179.         this.getContentPane().add(jp1,"North");
  180.  
  181.         label1.setText("  Entry name:  "+ entryName);
  182.         label2.setText("  Name          :  "+ name);
  183.         jp2.setLayout(new GridLayout(2,1,0,10));
  184.         jp2.add(label1);
  185.         jp2.add(label2);
  186.         jp3.add(jp31, null);
  187.         jp31.add(label3);
  188.         jp31.add(txtQuantity);
  189.         jp31.add(jp311);
  190.         jp311.add(buttonP);
  191.         jp311.add(buttonM);
  192.         jp3.add(jp32, null);
  193.         jp32.add(label5);
  194.         jp32.add(txtConcent);
  195.  
  196.         jp31.setLayout(new GridLayout(3,1,17,0));
  197.  
  198.         jp32.setLayout(new GridLayout(3,1,1,0));
  199.         jp3.setLayout(gridLayout1);
  200.         this.getContentPane().add(jp2,"Center");
  201.         this.getContentPane().add(jp3,"South");
  202.         
  203.         // 2002.4 akira shiozawa
  204.         getSubstanceData();
  205.     }
  206.  
  207.  
  208.     /**
  209.      * Event of ActionEvent
  210.      * @param e     ActionEvent
  211.      * @return  void
  212.      */
  213.     public void actionPerformed(ActionEvent e) {
  214. //System.out.println( "actionPerformed" );
  215.         if(e.getSource()== buttonClose){
  216.             exit_actionPerformed();
  217.         }
  218. /**
  219.         else if(e.getSource()== timer){
  220.             if(subCode>= 0 ){
  221.                 double[] data = new double[3];
  222.                 int count = window.count;
  223.                 try{
  224.                     window.df.getSubstanceData(count,subCode,data);
  225.                 }catch(Exception ex){
  226.                     ex.printStackTrace();
  227.                 }
  228.                 if(focusFlag1==false){
  229.                     txtQuantity.setText("   "+ getDoubleValue(data[0]));
  230.                 }
  231.                 if(focusFlag2==false){
  232.                     txtConcent.setText("   "+ getDoubleValue(data[1]));
  233.                 }
  234.                 if(data[2]==0.0){
  235.                     checkFix.setSelected(false);
  236.                 }else{
  237.                     checkFix.setSelected(true);
  238.                 }
  239.             }
  240.         }
  241. **/
  242.         else if(e.getSource() == buttonP){
  243.             try{
  244.                 window.df.setJniChange(subCode,1);
  245.             }catch(Exception ex){
  246.                 window.messageWindow.setErrorMessage( ex );
  247.             }
  248.             this.getSubstanceData();
  249.         }
  250.         else if(e.getSource() == buttonM) {
  251.             try{
  252.                 window.df.setJniChange(subCode,-1);
  253.             }catch(Exception ex){
  254.                 window.messageWindow.setErrorMessage( ex );
  255.             }
  256.             this.getSubstanceData();
  257.         }
  258.         else if(e.getSource() == checkFix){
  259.             try{
  260.                 window.df.setSubstanceFix( subCode,checkFix.isSelected());
  261.             }catch(Exception ex){
  262.                 window.messageWindow.setErrorMessage( ex );
  263.             }
  264.             this.getSubstanceData();
  265.         }
  266.         
  267.  
  268.     }
  269.  
  270.  
  271.     /**
  272.      * Event
  273.      * @param evt   ItemEvent
  274.      * @return void
  275.      */
  276.     public void itemStateChanged(ItemEvent evt){
  277.         try{
  278.             window.df.setSubstanceFix( subCode,checkFix.isSelected());
  279.         }catch(Exception ex){
  280.             window.messageWindow.setErrorMessage( ex );
  281.         }
  282.     }
  283.  
  284.     /**
  285.      * Event
  286.      * @param win   MainWindow
  287.      * @return void
  288.      */
  289.     public void accessJni( MainWindow win )
  290.     {
  291.         setRunningFlag( true );
  292.         this.getSubstanceData();
  293.         setRunningFlag( false );
  294.     }
  295.  
  296.  
  297.     private void getSubstanceData()
  298.     {
  299.         if(subCode>= 0 )
  300.         {
  301.             double[] data = new double[3];
  302.             int count = window.count;
  303.             try
  304.             {
  305.                    window.df.getSubstanceData(count,subCode,data);
  306.             }
  307.             catch(Exception ex)
  308.             {
  309.                 window.messageWindow.setErrorMessage( ex );
  310.             }
  311.             
  312.             //if(focusFlag1==false){
  313.             if( !txtQuantity.hasFocus() )
  314.             {
  315.                 txtQuantity.setText("   "+ getDoubleValue(data[0]));
  316.             }
  317.             //if(focusFlag2==false)
  318.             if( !txtConcent.hasFocus() )
  319.             {
  320.                 txtConcent.setText("   "+ getDoubleValue(data[1]));
  321.             }
  322.             
  323.             if(data[2]==0.0)
  324.             {
  325.                 checkFix.setSelected(false);
  326.             }
  327.             else
  328.             {
  329.                 checkFix.setSelected(true);
  330.             }
  331.         }
  332.     }
  333.  
  334.     /**
  335.      * Override to exit when the window is closed.
  336.      * @param e     WindowEvent
  337.      * @return void
  338.      */
  339. /**
  340.      protected void processWindowEvent(WindowEvent e) {
  341.         super.processWindowEvent(e);
  342. //System.out.println( "processWindowEvent" );
  343.         if (e.getID() == WindowEvent.WINDOW_CLOSING) {
  344.             exit_actionPerformed();
  345.  
  346.         }
  347.     }
  348. **/
  349.  
  350.     // 2002.4 akira shiozawa
  351.     KeyAdapter myKeyAdapter = new KeyAdapter()
  352.     {
  353.         public void keyPressed( KeyEvent e )
  354.         {
  355.             if( e.getKeyCode() == KeyEvent.VK_ESCAPE )
  356.             {
  357.                 JTextField dst = (JTextField)e.getSource();
  358.                 dst.setText( value_backup );
  359.             }
  360.         }
  361.     };
  362.  
  363.     /**
  364.      * Exit
  365.      * @return void
  366.      */
  367.     public void exit_actionPerformed(){
  368. //System.out.println( "Substance exit" );
  369.         //timer.stop();
  370.         // 2002.4. akira shizoawa
  371.         window.removeAccessListener( this );
  372.  
  373.         try{
  374.             window.df.setSubstanceState(subCode,-1);
  375.         }catch(Exception ex){
  376.             window.messageWindow.setErrorMessage( ex );
  377.         }
  378.         this.setVisible(false);
  379.  
  380. /**
  381.         Vector vs = window.vSubstance;
  382.         for(int i=0;i<vs.size();i++){
  383.             if(this==vs.elementAt(i)){
  384.                 vs.remove(i);
  385.                 //this.dispose();
  386.             }
  387.         }
  388. **/
  389.  
  390.         this.dispose();
  391.  
  392.     }
  393.  
  394.     /**
  395.      * focusGained event
  396.      * @param e     FocusEvent
  397.      * @return void
  398.      */
  399.     public void focusGained(FocusEvent e){
  400. //System.out.println( "focusGained" );
  401.         value_backup = ((JTextField)e.getSource()).getText();
  402. /**
  403.         if(e.getSource()==txtQuantity){
  404.             //focusFlag1=true;
  405.             txtQuantity_backup = txtQuantity.getText();
  406.         }
  407.         if(e.getSource()==txtConcent){
  408.             //focusFlag2=true;
  409.             txtConcent_backup = txtConcent.getText();
  410.         }
  411. **/
  412.     }
  413.  
  414.     /**
  415.      * forcusLost event
  416.      * @param e  FocusEvent
  417.      * @return void
  418.      */
  419.     public void focusLost(FocusEvent e) {
  420.         double data =0.0;
  421.         
  422.         // 2002.4  akira shiozawa
  423.         boolean changed = false;
  424.         String input_value = ((JTextField)e.getSource()).getText();
  425.         if( !input_value.equals( value_backup ) )
  426.         {
  427.             try
  428.             {
  429.                 changed = true;
  430.                 data = Double.parseDouble( input_value );
  431.                 window.df.setSubstanceText( subCode, ((e.getSource() == txtConcent)? 1 : 0), data );
  432.             }
  433.             catch( Exception ex )
  434.             {
  435.                 window.messageWindow.setErrorMessage( ex );
  436.             }
  437.         }
  438.  
  439. /**
  440.         // 2002.1.23 akira shiozawa
  441.         boolean changed = false;
  442.         if(e.getSource()==txtQuantity){
  443.             try{
  444.                 if( !txtQuantity.getText().equals( txtQuantity_backup ) )
  445.                 {
  446.                     changed = true;
  447.                       data=Double.parseDouble(txtQuantity.getText());
  448. //System.out.println( "quantity text:" + data );
  449.                     window.df.setSubstanceText(subCode,0,data);
  450.                 }
  451.             }catch(Exception ex){
  452.             }
  453.             //focusFlag1=false;
  454.         }
  455.  
  456.         if(e.getSource()==txtConcent){
  457.             try{
  458.                 if( !txtConcent.getText().equals( txtConcent_backup ) )
  459.                 {
  460.                     changed = true;
  461.                     data=Double.parseDouble(txtConcent.getText());
  462. //System.out.println( "concentration text:" + data );
  463.                     window.df.setSubstanceText(subCode,1,data);
  464.                 }
  465.             }catch(Exception ex){
  466.             }
  467.             
  468.             //focusFlag2=false;
  469.         }
  470. **/
  471.  
  472.         // 2002.1.23 akira shiozawa
  473.         if( changed )
  474.         {
  475.             double[] value = new double[3];
  476.             try{
  477.                    window.df.getSubstanceData(window.count,subCode,value);
  478.             }
  479.             catch(Exception ex)
  480.             {
  481.                 window.messageWindow.setErrorMessage( ex );
  482.             }
  483.             txtQuantity.setText("   "+ getDoubleValue(value[0]));
  484.             txtConcent.setText("   "+ getDoubleValue(value[1]));
  485.         }
  486.     }
  487.  
  488.      /**
  489.      * Convert Double value to suitable String one.
  490.      * @param vaule     Double data
  491.      * @return     Converted String
  492.      */
  493.     public String getDoubleValue(double value){
  494.         int dotIndex=-1;
  495.         int eIndex=-1;
  496.  
  497.         String str=String.valueOf(value);
  498. //System.out.println( "str:" + str + ",double:" + value );
  499.         int len=str.length();
  500.         if(len<=8) return str;
  501.  
  502.  
  503.         dotIndex=str.indexOf(".");
  504.         eIndex=str.indexOf("E");
  505.         if(eIndex==-1){
  506.             if(dotIndex==-1){
  507.                 return str;
  508.             }else if(dotIndex==7){
  509.                 return str.substring(0,9);
  510.             }else if(dotIndex<7){
  511.                 return str.substring(0,8);
  512.             }else if(dotIndex>7){
  513.                 return str.substring(0,dotIndex);
  514.             }
  515.         }else if(eIndex<=7){
  516.             return str;
  517.         }else{
  518.             return (str.substring(0,7)+str.substring(eIndex,len)); 
  519.         }
  520.         return "";
  521.  
  522.     }
  523.  
  524. }
  525.